import { TranslationComponent,TranslationComponentV1} from '../types';
import apiClient from './client';

// 获取翻译组件列表
export const getTranslationComponents = async (page: number = 1, pageSize: number = 10): Promise<{ code: number, data: TranslationComponent[], message: string, total: number }> => {
  const response = await apiClient.get('/lan/v1/element/trans/list', {
    params: { page, pageSize },
  });
  console.log('API Response:', response.data); // 添加日志来调试
  return response.data;
};
// 创建组件
export const createComponent = async (componentData: Omit<TranslationComponent, 'id'>): Promise<TranslationComponent> => {
  const formattedData = {
    name: componentData.name,
    type: componentData.type,
    dsc: componentData.description,
    translations: componentData.translations || {},
  };
  const response = await apiClient.post('/lan/v1/element/save', formattedData);
  // 修改: 将后端返回的 dsc 字段映射为 description 字段
  return {
    ...response.data,
    description: response.data.dsc,
  };
};
// 编辑组件
export const editComponent = async (componentData: Partial<TranslationComponentV1>): Promise<TranslationComponentV1> => {
  if (!componentData.id) {
    throw new Error('请填写表格');
  }
  const formattedData = {
    id: componentData.id,
    name: componentData.name,
    type: componentData.type,
    dsc: componentData.description,
    translations: componentData.translations || {},
  };
  const response = await apiClient.post('/lan/v1/element/save', formattedData);
  // 修改: 将后端返回的 dsc 字段映射为 description 字段
  return {
    ...response.data,
    description: response.data.dsc,
  };
};

// 获取单个组件
export const getComponent = async (id: string): Promise<TranslationComponent> => {
  const response = await apiClient.get(`/components/${id}`);
  return response.data;
};

// 更新组件
export const updateComponent = async (id: string, componentData: Partial<TranslationComponent>): Promise<TranslationComponent> => {
  const response = await apiClient.put(`/components/${id}`, componentData);
  return response.data;
};

// 删除组件
export const deleteComponent = async (id: string): Promise<void> => {
  await apiClient.delete(`/lan/v1/element/${id}`);
};

// 更新组件翻译
export const updateComponentTranslation = async (
  id: string,
  languageCode: string,
  translation: string
): Promise<TranslationComponent> => {
  const response = await apiClient.patch(`/components/${id}/translations`, {
    languageCode,
    translation,
  });
  return response.data;
};

// 删除组件翻译
export const deleteTranslation = async (element_id: string, lang_id: string): Promise<void> => {
  await apiClient.delete('/lan/v1/trans/', {
    data: { element_id, lang_id },
  });
};

// 新增导出函数
export const exportTranslation = async (): Promise<{ code: number, data: Blob, message?: string }> => {
  const response = await apiClient.get('/lan/v1/element/export', {
    responseType: 'blob', // 设置响应类型为 Blob，用于处理文件下载
  });
  return {
    code: response.status,
    data: response.data,
    message: response.data.msg, // 假设后端返回的 msg 字段包含提示信息
  };
};

// 新增: 导入翻译文件
export const importTranslation = async (file: File): Promise<{ code: number, message: string }> => {
  const formData = new FormData();
  formData.append('file', file);

  const response = await apiClient.post('/lan/v1/element/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });

  return response.data;
};
